/*
 * ExtentQueryHistory.cpp
 *
 *  Created on: Dec 23, 2011
 *      Author: Netali
 */

#include "ExtentQueryHistory.h"

ExtentQueryHistory::ExtentQueryHistory(){
	extentPerTime = new map<int,ExtentTime*>();
}

ExtentQueryHistory::ExtentQueryHistory(SystemView* sys){
	set<ExtentEntry*>* entries = sys->getMy_entries();
	set<ExtentEntry*>::iterator entriesIterator;
	map<int,ExtentTime*>::iterator extentPerTimeIterator;
	extentPerTime = new map<int,ExtentTime*>();

	for(entriesIterator =entries->begin(); entriesIterator != entries->end(); entriesIterator++){
		int time = ((ExtentEntry*)(*entriesIterator))->getMy_time();
		Extent* ex = ((ExtentEntry*)(*entriesIterator))->getMy_extent();
		extentPerTimeIterator = extentPerTime->find(time);
		if (extentPerTimeIterator == extentPerTime->end()){ //the time unit isn't exist
			ExtentTime* et = new ExtentTime(time);
			et->addExtent(ex);
			extentPerTime->insert(pair<int,ExtentTime*>(time,et));
		}
		else{//the time unit already exist
			extentPerTime->at(time)->addExtent(ex);
		}
	}
}

ExtentQueryHistory::~ExtentQueryHistory(){
	map<int,ExtentTime*>::iterator extentPerTimeIterator;
	for(extentPerTimeIterator = extentPerTime->begin(); extentPerTimeIterator != extentPerTime->end(); extentPerTimeIterator++){
		delete extentPerTimeIterator->second;
	}
	delete extentPerTime;
}

//assuming time is one of the time units that given us in the input file
int ExtentQueryHistory::notIncludeInPrevTimeUnit(int time){
	int num = 0;
	int prevTime = -1;
	map<int,ExtentTime*>::iterator it1;
	ExtentTime* now;
	ExtentTime* prev;

	//finding previous time unit
	for(it1 = extentPerTime->begin(); it1 != extentPerTime->end(); it1++){
		if((*it1).first < time && (*it1).first > prevTime ){
			prevTime = (*it1).first;
		}
	}
	if (prevTime != -1){//getting the extentTime instances if the time unit isn't the first one
		now = extentPerTime->at(time);
		prev = extentPerTime->at(prevTime);
		set<Extent*>* tmp = now->getExtents();
		set<Extent*>::iterator it2;
		for (it2 = tmp->begin(); it2 != tmp->end(); it2++){
			Extent* ex = ((Extent*) (*it2));
			if (!prev->isExist(ex))// extent wasn't exist in previous time unit
				num++;
		}
	}
	else
		return extentPerTime->at(time)->getExtents()->size(); //when time is the first time unit so all the extents were not included in the previous time unit
	return num;
}

int ExtentQueryHistory::notSeenYet(int time){
	int num = 0;
	map<int,ExtentTime*>::iterator it1;
	bool isFirst = true;

	//finding num of extents that weren't exists in all previous time units
	for(it1 = extentPerTime->begin(); it1 != extentPerTime->end(); it1++){
		if(((*it1).first < time) ){
			isFirst = false;
			ExtentTime* prevExTime = (*it1).second;
			ExtentTime* nowExTime = extentPerTime->at(time);
			set<Extent*>* tmp = nowExTime->getExtents();
			set<Extent*>::iterator it2;
			for (it2 = tmp->begin(); it2 != tmp->end(); it2++){
				Extent* ex = *it2;
				if (!prevExTime->isExist(ex))// extent wasn't exist in previous time unit
					num++;
			}
		}
	}
	if (isFirst)
		return extentPerTime->at(time)->getExtents()->size(); //when time is the first time unit so all the extents were not included in the previous time unit
	return num;
}

string ExtentQueryHistory::toString(){
	string str = "";
	map<int,ExtentTime*>::iterator extentPerTimeIterator;
	for(extentPerTimeIterator = extentPerTime->begin(); extentPerTimeIterator != extentPerTime->end(); extentPerTimeIterator++){
		ExtentTime* tEx = (*extentPerTimeIterator).second;
		str.append(tEx->toString());
		str.append("\n");
	}
	return str;
}

